using System;
using System.Windows.Input;

namespace Empanada
{
    public class DelegateCommand : ICommand
    {
        private readonly Action _execute;
        private bool _canExecute;

        public DelegateCommand(Action execute, bool canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }

        void ICommand.Execute(object parameter)
        {
            _execute();
        }

        bool ICommand.CanExecute(object parameter)
        {
            return _canExecute;
        }

        public void SetCanExecute(bool canExecute)
        {
            if (_canExecute == canExecute) return;

            _canExecute = canExecute;

            var handler = CanExecuteChanged;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        public event EventHandler CanExecuteChanged;
    }
}